/**
 * Copyright 2016-2018 mobaas.com
 */
package com.mobaas.devops.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mobaas.devops.dao.VcsStatDao;
import com.mobaas.devops.model.VcsDayStat;
import com.mobaas.devops.model.VcsMonthStat;
import com.mobaas.devops.service.VcsStatService;

/**
 * 
 * @author billy zhang
 * 
 */
@Service
public class VcsStatServiceImpl implements VcsStatService{

	@Autowired
	private VcsStatDao statDao;

	@Override
	public List<String> selectVcsUserList(Date beginDate, Date endDate) {
		return statDao.selectVcsUserList(beginDate, endDate);
	}

	@Override
	public List<VcsDayStat> selectDayStatList(Date beginDate, Date endDate) {
		return statDao.selectDayStatList(beginDate, endDate);
	}

	@Override
	public List<VcsDayStat> selectDayStatListByUser(String user, Date beginDate, Date endDate) {
		return statDao.selectDayStatListByUser(user, beginDate, endDate);
	}

	@Override
	public List<VcsDayStat> selectDayStatListForDayMax( Date beginDate, Date endDate) {
		return statDao.selectDayStatListForDayMax(beginDate, endDate);
	}

	@Override
	public List<VcsMonthStat> selectMonthStatList(int limit) {
		return statDao.selectMonthStatList(limit);
	}
	
	@Override
	@Transactional
	public void insertDayStat(List<VcsDayStat> list) {
		for (VcsDayStat ss : list) {
			VcsDayStat old = statDao.selectDayStatByUserAndDate(ss.getUser(), ss.getStatDate());
			if (old != null) {
				old.setFileTotal( ss.getFileTotal() );
				statDao.updateDayStat(old);
			} else {
				statDao.insertDayStat(ss);
			}
		}
	}
	
	@Override
	@Transactional
	public void insertMonthStat(List<VcsMonthStat> list) {
		for (VcsMonthStat ss : list) {
			VcsMonthStat old = statDao.selectMonthStatByUserAndMonth(ss.getUser(), ss.getStatMonth());
			if (old != null) {
				old.setFileTotal( ss.getFileTotal() );
				statDao.updateMonthStat(old);
			} else {
				statDao.insertMonthStat(ss);
			}
		}
	}

	@Override
	public List<VcsDayStat> selectDayStatListByDate(Date date) {
		return statDao.selectDayStatListByDate(date);
	}

	@Override
	public List<VcsMonthStat> selectMonthStatListByMonth(String month) {
		return statDao.selectMonthStatListByMonth(month);
	}
	
}
